package com.janko.springbootbase.designmode.prototypeClonePatten;

/**
 * @Author nyk
 * @Date 2021/4/30 8:53
 * @Version 1.0
 * @Desc
 */
public class TestClone {
    /**
     * 浅拷贝：对象属性拷贝了一遍，但是源对象与新对象的 引用地址都是同一个
     * 深拷贝： 属性值和引用都被拷贝了一遍新的，属性引用地址也是新的地址，两个对象HashCode不一样
     */

    public static void main(String[] args) throws CloneNotSupportedException {
        Subject subject=new Subject("语文",1);
        StudentEasy myQian=new StudentEasy("1","张三",subject);
        StudentEasy easyCopy=(StudentEasy)myQian.clone();
        System.out.println("浅克隆源数据引用的Subject地址是："+myQian.getSubject().hashCode());
        System.out.println("浅克隆源数后引用的Subject地址是："+easyCopy.getSubject().hashCode());

        subject.setSubjectName("英语");
        System.out.println("更改学科后浅克隆源是："+myQian.toString());
        System.out.println("更改学科后浅克隆源是:"+easyCopy.toString());



        System.out.println("\n----->深克隆开始<----------\n");
        Subject subjectForDeep=new Subject("数学",2);
        StudentDeep deep=new StudentDeep("1","deep",subjectForDeep);
        StudentDeep deepCopy=(StudentDeep)deep.clone();
        System.out.println("深克隆源数据引用的Subject地址是："+deep.getSubject().hashCode());
        System.out.println("深克隆源数后引用的Subject地址是："+deepCopy.getSubject().hashCode());

        subjectForDeep.setSubjectName("历史");
        System.out.println("更改学科后deep克隆源是："+deep.toString());
        System.out.println("更改学科后deep克隆源是:"+deepCopy.toString());
    }
}
